﻿import { Component, Injector, ViewChild ,Input} from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { finalize } from 'rxjs/operators';
import { Tab } from './tab.model';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { RequirementCommissionItemEditComponent } from './requirement-commission-item_edit.component';
import { RequirementCommissionComponent } from './requirement-commission.component';
import { TestDataDto } from '@shared/service-proxies/service-proxies';
import { TaskForStandardPartsServiceProxy, TaskForStandardPartsDto } from '@shared/service-proxies/service-proxies';

@Component({
    selector: 'requirementCommissionTabComponent',
    templateUrl: './requirement-commission_tab.component.html',
    styleUrls: ['./requirement-commission_tab.component.less'],
    animations: [appModuleAnimation()]
})
export class RequirementCommissionTabComponent extends AppComponentBase{
    @ViewChild('requirementCommissionItemEditComponent') requirementCommissionItemEditComponent: RequirementCommissionItemEditComponent;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    @Input() standardPartsDetailId: string;

    tabs: Array<Tab>;
    tabObject: {
        filterText: ''
    };
    loading = false;
    caches: any = null;
    logs: any = '';
    filterText = '';

    active = false;
    hidden = true;
    title: 'hello';

    @Input() parentComponent: RequirementCommissionComponent;

    constructor(
        injector: Injector,
        private _askForStandardPartsServiceProxy: TaskForStandardPartsServiceProxy) {
        super(injector);
        this.active = false;
    }
    /**
     * 设置父窗口
     * @param _pParentComponent
     */
    setParentComponent(_pParentComponent: RequirementCommissionComponent) {
        this.parentComponent = _pParentComponent;
    }
    /**
     * 刷新父窗口
     */
    refreshParentComponent() {
        if (this.parentComponent) {
            this.parentComponent.refresh()
        }
    }
    /**
     * 刷新本页面列表
     */
    refresh(){
        this.getRequirementItems(null);
    }
    addTab(testDataDto: TestDataDto): void {
        this.active = true;
    }

    createOrEditRequirementItem(taskForStandardPartsDto : TaskForStandardPartsDto):void{
        this.requirementCommissionItemEditComponent.linkEdit(taskForStandardPartsDto);
    }

    setStandardPartsDetailId(_pStandardPartsDetailId):void{
        this.standardPartsDetailId = _pStandardPartsDetailId;
    }
    deleteRequirementPackage(testDataDto: TestDataDto): void {
        this.message.confirm(
            '需求包将被删除',
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                    this._askForStandardPartsServiceProxy.deleteTask(testDataDto.id)
                    .pipe(finalize(() => { this.getRequirementItems();}))
                    .subscribe(() => {
                        this.notify.info('删除成功！');
                    });
                }
            }
        );
    }
    getRequirements( tab: Tab, event?: LazyLoadEvent) {
        this.primengTableHelper.showLoadingIndicator();
    }
    getRequirementItems(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this.primengTableHelper.records = [];
        this.primengTableHelper.showLoadingIndicator();
        this._askForStandardPartsServiceProxy.getAllTasksByDetailId(this.standardPartsDetailId)
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                if (result == null) {
                    result = [];
                }
                this.primengTableHelper.totalRecordsCount = result.length;
                this.primengTableHelper.records = result;
                this.primengTableHelper.hideLoadingIndicator();
            });

    }
    deleteRequirementItems(taskForStandardPartsDto: TaskForStandardPartsDto){
        this.message.confirm(
            '删除该需求？',
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                    this._askForStandardPartsServiceProxy.deleteTask(taskForStandardPartsDto.id)
                    .pipe(finalize(() => { this.getRequirementItems();}))
                    .subscribe(() => {
                        this.refreshParentComponent();
                        this.notify.info(this.l('DeletedSuccessfully'));
                    });
                }
            }
        );
    }
    downloadWebLogs = function () {
        const self = this;
        self._webLogService.downloadWebLogs().subscribe((result) => {
            self._fileDownloadService.downloadTempFile(result);
        });
    };

    onResize(event): void {

    }
    ngOnInit(): void {
        const self = this;
    }
}
